package com.shop.service.impl;

import com.shop.dao.AdminDao;
import com.shop.entity.AdminUser;
import com.shop.entity.Result;
import com.shop.service.AdminService;
import com.shop.util.ResultUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * description: 该文件说明
 *
 * @author zhaoxiaozhao（xiaozhao.zhao@ucarinc.com）
 * @version 1.0
 * @date 2018/10/16 15:16
 **/
@Service
public class AdminServiceImp implements AdminService {

    @Resource
    private AdminDao adminDao;
    /**
     * description:<登陆>
     *
     * @author zhaoxiaozhao（xiaozhao.zhao@ucarinc.com）
     * @date   2018/10/16 15:16
     * @param  
     * @return 
     **/
    @Override
    public Result login(AdminUser adminUser) {
        Result result = checkDate(adminUser);
        if(ResultUtil.ERROR == result.getCode()){
            return result;
        }
        try {
            adminUser = adminDao.findAdminUser(adminUser);
        } catch (Exception e) {
            return ResultUtil.error("系统出错，登陆失败");
        }
        if(adminUser == null){
            return ResultUtil.error("登陆失败，请检查账号密码是否正确");
        }
        return ResultUtil.success("登陆成功");
    }
    /**
     * description:<数据校验>
     *
     * @author zhaoxiaozhao（xiaozhao.zhao@ucarinc.com）
     * @date   2018/10/16 15:18
     * @param  
     * @return 
     **/
    private Result checkDate(AdminUser adminUser){
        if(null == adminUser){
            return ResultUtil.error("登陆失败，请检查账号和密码");
        }
        if(StringUtils.isEmpty(adminUser.getAdminName())){
             return ResultUtil.error("登陆失败，用户名不能为空");
        }
        if(StringUtils.isEmpty(adminUser.getAdminPassword())){
            return ResultUtil.error("登陆失败，密码不能为空");
        }
        return ResultUtil.success();
    }
}
